package com.sky.controller.user;


import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "地址铺相关接口")
@RestController
@RequestMapping("/user/addressBook")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;


    /**
     * 添加地址数据
     * @return
     */
    @PostMapping
    @ApiOperation("添加地址数据")
    public Result save(@RequestBody AddressBook addressBook){
        log.info("添加地址数据{}",addressBook);
        addressBookService.save(addressBook);
        return Result.success();
    }

    /**
     * 查询该用户的地址铺数据
     * @return
     */
    @ApiOperation("查询该用户的地址铺数据")
    @GetMapping("/list")
    public Result<List<AddressBook>> list(){
        log.info("查询该用户的地址铺数据");
        List<AddressBook> addressBooks = addressBookService.list();
        return Result.success(addressBooks);
    }

    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询数据")
    public Result<AddressBook> getById(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);
    }

    /**
     * 根据id修改数据
     * @param addressBook
     * @return
     */
    @PutMapping
    @ApiOperation("根据id修改数据")
    public Result update(@RequestBody AddressBook addressBook){

        addressBookService.update(addressBook);
        return Result.success();
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @ApiOperation("设置默认地址")
    @PutMapping("/default")
    public Result setDefault(@RequestBody AddressBook addressBook){
        log.info("设置默认地址 {}",addressBook);
        addressBookService.setDefault(addressBook.getId());
        return Result.success();
    }

    /**
     * 查询默认地址
     * @return
     */
    @ApiOperation("查询默认地址")
    @GetMapping("/default")
    public Result<AddressBook> getDefault(){

        AddressBook addressBook = addressBookService.getDefault();
        return Result.success(addressBook);
    }

}
